var file_util = require('../public/file_util');
var path = require('path');
const Define = require('./ExportDefine')
function CSParser(sheet) {

    this.sheet = sheet;
}

CSParser.prototype.getModuleName = function(){
    return this.sheet.getModuleName()
}

CSParser.prototype.parseEnum = function () {
    var sheetName = this.sheet.sheetName
    // console.log(" sheetname", sheetName);
    let CR = Define.CR;
    //生成枚举。
    var enumName = sheetName.substring(0, 1).toUpperCase() + sheetName.substring(1);
    this.fileNames = enumName + ': ' + "'" + this.sheet.sheetName + "',"
    var declareObj = 'declare class Json' + enumName + ' {' + CR + '\t%{data}' + CR + '}'
    var enumObj = "public enum " + enumName + "Index {" + CR + "\t%{data}" + CR + "}";
    var enums = "";
    var declares = ''
    // var keyIndex = 0;
    let keyArray = this.sheet.keyArray;
    let typeArray = this.sheet.typeArray;
    let nameArray = this.sheet.nameArray;
    for (var k = 0; k < keyArray.length; k++) {
        if (typeArray[k] && typeArray[k] != 'null') {
            var key = keyArray[k];
            let type = typeArray[k]
            if (key != undefined) {
                let tname = Define.CS_TYPE[type.trim()];
                if (!tname) {
                    console.error(' tname is null type is ', type)
                    continue;
                }
                if (nameArray[k]) {
                    let temp = nameArray[k].replace(/\n/g, '') + "" + Define.CR + "\t";
                    enums += key.toUpperCase() + ",// " + temp;
                    declares += key + ':' + tname + ";// " + temp;
                } else {
                    enums += key.toUpperCase() + "," + "" + Define.CR + "\t";
                    declares += key + ':' + tname.toUpperCase() + ";" + "" + Define.CR + "\t";
                }
                // this.nameList[keyIndex] = key
                // this.typeList[keyIndex] = typeArray[k]
                // if (dbArray && dbArray[k]) {
                //     this.dbList[keyIndex] = dbArray[k]
                // } else {
                //     // console.warn('k is ', k)
                // }
                // this.indexOfKey[key] = keyIndex++
            }

        }
    }
    // console.log(" indexOfKey", this.indexOfKey);
    enumObj = enumObj.replace('%{data}', enums);
    this.enumObj = enumObj;
    this.enumName = enumName;
}

CSParser.prototype.parseEnumDefine = function () {
    if (this.sheet.getExportType() != Define.FILE_TYPE_ENUM) {
        return ;
    }
    var sheetName = this.sheet.sheetName
    // console.log(' sheetName ', sheetName)
    var enumName = sheetName.substring(0, 1).toUpperCase() + sheetName.substring(1);
    // console.log(' enumName ', enumName)
    var enumObj = "public enum " + enumName + "Enum {" + Define.CR + "\t%{data}" + Define.CR + "}";
    var enums = "";
    var idIndex = this.sheet.getKeyIndex('ID')
    if (idIndex == -1) {
        idIndex = this.sheet.getKeyIndex('id')
        if (idIndex == -1) {
            idIndex = this.sheet.getKeyIndex('Id')
        }
    }
    var nameIndex = this.sheet.getKeyIndex('name')

    var descIndex = this.sheet.getKeyIndex('desc')

    // console.log(' idIndex ', idIndex, 'nameIndex ', nameIndex)
    if (idIndex >= 0 && nameIndex >= 0) {
        for (var j = Define.DATA_START; j < this.sheet.excleData.length; j++) {
            var curData = this.sheet.excleData[j];
            // console.log(' curData ', curData)
            // console.log(' curData.length ', curData.length)
            if (curData.length == 0 || curData[nameIndex] == undefined || curData[idIndex] == undefined) continue;
            // console.log(' curData[nameIndex] ', curData[nameIndex], '  curData[idIndex]  ', curData[idIndex])
            let desc = descIndex >= 0 ? curData[descIndex] : '';
            enums += curData[nameIndex] + " = " + curData[idIndex] + ",// " + desc + "" + Define.CR + "\t";
            // console.log(' enums ', enums)
        }
    }
    // console.log(' enums ', enums)
    enumObj = enumObj.replace('%{data}', enums);
    this.enumDefine = enumObj;
    this.enumDefineName = enumName;

}

CSParser.prototype.parseClass = function (manager, template) {
    if (!template) {
        console.warn('template is null ')
        return;
    }
    let sheetName = this.sheet.sheetName;
    var className = sheetName.substring(0, 1).toUpperCase() + sheetName.substring(1);
    let enumName = className + "Index"
    this.className = className;
    let TemplateName = 'Json' + className;
    let classObj = template.replace(/%ClassName%/g, className)
    classObj = classObj.replace('%SheetName%', this.sheet.sheetName)
    classObj = classObj.replace('%enum%', this.enumObj)

    classObj = classObj.replace('%TemplateName%', TemplateName)

    let exportStr = ''
    let keyArray = this.sheet.keyArray;
    let nameArray = this.sheet.nameArray;
    let typeArray = this.sheet.typeArray;
    for (var k = 0; k < keyArray.length; k++) {
        if (typeArray[k] != 'null') {
            let tname = Define.CS_TYPE[typeArray[k].trim()];
            if (!tname) {
                console.log(' tname is null type is ', typeArray[k].trim(), className, typeArray[k].trim())
                console.error(' tname is null type is ')
                tname = ''
            }
            var key = "// ";
            if (nameArray[k]) {
                key += nameArray[k].replace(/\n/g, '')
            }
            key += Define.CR + "\t"; //说明
            if (typeof (keyArray[k]) == "string") {
                let funcName = keyArray[k].substring(0, 1).toUpperCase() + keyArray[k].substring(1);
                key += "public " + tname + ' Get' + funcName + "() " + Define.CR + "\t{" + Define.CR + "\t\t"; //getkeyArray[k](){
                key += 'return (' + tname + ') this.cfg[(int)' + enumName + "." + keyArray[k].toUpperCase() + '] ;' + Define.CR + '\t';
                key += '}' + Define.CR + '\t'
                exportStr += key;
            }



        }
    }
    classObj = classObj.replace('%function%', exportStr)
    this.classObj = classObj
    // console.log(' classObj ', classObj)
}

CSParser.prototype.parse = function (manager, template) {
    this.parseEnum();
    this.parseEnumDefine();
    this.parseClass(manager,template)
}
module.exports = CSParser; 
